////////////////////////////////////////////////////////////////////////////////
// Copyright 2010 Michael Schmalle - Teoti Graphix, LLC
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0 
// 
// Unless required by applicable law or agreed to in writing, software 
// distributed under the License is distributed on an "AS IS" BASIS, 
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and 
// limitations under the License
// 
// Author: Michael Schmalle, Principal Architect
// mschmalle at teotigraphix dot com
////////////////////////////////////////////////////////////////////////////////

package org.teotigraphix.swizplugin.registry.core
{

import org.teotigraphix.swizplugin.registry.IContributor;
import org.teotigraphix.swizplugin.registry.IExtension;
import org.teotigraphix.swizplugin.registry.IExtensionDescriptor;
import org.teotigraphix.swizplugin.registry.IExtensionPoint;

/**
 * The internal implementation of the <code>IExtensionPoint</code> API.
 * 
 * @author Michael Schmalle
 * @copyright Teoti Graphix, LLC
 * @productversion 1.0
 */
public class RegistryExtensionPoint extends RegistryClient 
	implements IExtensionPoint
{
	public var contributorID:int;
	
	//--------------------------------------------------------------------------
	//
	//  IExtension API :: Properties
	//
	//--------------------------------------------------------------------------
	
	//----------------------------------
	//  contributor
	//----------------------------------
	
	/**
	 * @copy swizplugin.registry.IExtensionPoint#contributor
	 */
	public function get contributor():IContributor
	{
		return registry.getContributor(contributorID);
	}
	
	//----------------------------------
	//  simpleID
	//----------------------------------
	
	/**
	 * @private
	 */
	private var _simpleID:String;
	
	/**
	 * @copy swizplugin.registry.IExtensionPoint#simpleID
	 */
	public function get simpleID():String
	{
		return _simpleID;
	}
	
	/**
	 * @private
	 */
	public function set simpleID(value:String):void
	{
		_simpleID = value;
	}
	
	//----------------------------------
	//  namespaceID
	//----------------------------------
	
	/**
	 * @private
	 */
	private var _namespaceID:String;
	
	/**
	 * @copy swizplugin.registry.IExtensionPoint#namespaceID
	 */
	public function get namespaceID():String
	{
		return _namespaceID;
	}
	
	/**
	 * @private
	 */
	public function set namespaceID(value:String):void
	{
		_namespaceID = value;
	}
	
	//----------------------------------
	//  uniqueID
	//----------------------------------
	
	/**
	 * @private
	 */
	private var _uniqueID:String;
	
	/**
	 * @copy swizplugin.registry.IExtensionPoint#uniqueID
	 */
	public function get uniqueID():String
	{
		return _uniqueID;
	}
	
	/**
	 * @private
	 */
	public function set uniqueID(value:String):void
	{
		_uniqueID = value;
		if (_uniqueID != null)
		{
			_simpleID = _uniqueID.substr(
				_uniqueID.lastIndexOf(".") + 1);
		}
	}
	
	//----------------------------------
	//  internalID
	//----------------------------------
	
	/**
	 * @copy swizplugin.registry.IExtensionPoint#internalID
	 */
	public function get internalID():int
	{
		return registryID;
	}
	
	//----------------------------------
	//  label
	//----------------------------------
	
	/**
	 * @private
	 */
	private var _label:String;
	
	/**
	 * @copy swizplugin.registry.IExtensionPoint#label
	 */
	public function get label():String
	{
		return _label;
	}
	
	/**
	 * @private
	 */
	public function set label(value:String):void
	{
		_label = value;
	}
	
	//--------------------------------------------------------------------------
	//
	//  Constructor
	//
	//--------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 * @private
	 */
	public function RegistryExtensionPoint(registry:ExtensionRegistry, 
										   registryID:int, 
										   children:Array = null)
	{
		super(registry, registryID);
		
		this.children = children;
	}
	
	//--------------------------------------------------------------------------
	//
	//  IExtensionPoint API :: Methods
	//
	//--------------------------------------------------------------------------
	
	/**
	 * @copy swizplugin.registry.IExtensionPoint#getExtensionDescriptors()
	 */
	public function getExtensionDescriptors():Array
	{
		var result:Array = [];
		var extensions:Array = getExtensions();
		
		for each (var extension:IExtension in extensions)
		{
			var descriptors:Array = extension.getExtensionDescriptors();
			for each (var descriptor:IExtensionDescriptor in descriptors)
			{
				result.push(descriptor);
			}
		}
		return result;
	}
	
	/**
	 * @copy swizplugin.registry.IExtensionPoint#getExtension()
	 */
	public function getExtension(extensionID:String):IExtension
	{
		if (extensionID == null)
		{
			return null;
		}
		
		var len:int = children.length;
		for (var i:int = 0; i < len; i++)
		{
			var ext:IExtension = registry.getRegistryExtension(children[i]);
			if (extensionID == ext.simpleID)
			{
				return ext;
			}
		}
		
		// FIXME 
		// this means that the id of the extension was not found in the
		// extension point's id using the extension's simpleID
		// for now we will try again and use the extension.uniqueID
		
		len = children.length;
		for (i = 0; i < len; i++)
		{
			ext = registry.getRegistryExtension(children[i]);
			if (extensionID == ext.uniqueID)
			{
				return ext;
			}
		}
		
		return null;
	}
	
	/**
	 * @copy swizplugin.registry.IExtensionPoint#getExtensions()
	 */	
	public function getExtensions():Array
	{
		return registry.getExtensions(_uniqueID);
	}
}
}